<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Order extends AdminController
{

    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\WriteLog();
    }
    public function index(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['write_log.platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['platformInfo.telphone','=',$param['telphone']];
        }
        if (isset( $param['store_telphone']) &&   $param['store_telphone'] ){
            $where[] = ['storeInfo.telphone|storeInfo.store_name','like',"%".$param['store_telphone']."%"];
        }
        if (isset( $param['user_name']) &&   $param['user_name'] ){
            $where[] = ['userInfo.nickname','=',$param['user_name']];
        }
        if (isset( $param['subsidy_status']) &&   $param['subsidy_status'] ){
            $where[] = ['subsidy_status','=',$param['subsidy_status']];
        }
        if (isset( $param['pay_status']) &&   $param['pay_status'] ){
            $where[] = ['pay_status','=',$param['pay_status']];
        }
        if (isset( $param['start_time']) &&   $param['start_time'] ){
            $where[] = ['order.create_time','>=',strtotime($param['start_time'])];
        }

        if (isset( $param['end_time']) &&   $param['end_time'] ){
            $where[] = ['order.create_time','<=',strtotime($param['end_time'])];
        }
        $count = $this->model->withJoin(['platformInfo','consumerInfo','userInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin(['platformInfo','consumerInfo','userInfo','storeInfo'], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as $key => $value){

        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $orderInfo = $this->model->where(['id' =>$param['id'] ])->find();
        if ($param['subsidy_status'] == 4){
            $this->model->where(['id' =>$param['id'] ])->update(['subsidy_status' => $param['subsidy_status']]);
             $this->model->where(['id' =>$orderInfo['activity_id'] ])->inc('review_number')->update();
            if ($orderInfo['subsidy_amount'] > 0){
                $storeModel= new \app\admin\model\Store();
                $storeModel->where(['id' => $orderInfo['store_id']])->inc('balance',$orderInfo['subsidy_amount'])->update();
            }
        }else if ($param['subsidy_status'] == 3){
            $this->model->where(['id' =>$param['id'] ])->update([
                'subsidy_status' => $param['subsidy_status'],
                'subsidy_reason' => $param['subsidy_reason'],
                ]);
        }else{
            return msg([

            ], 1, "无效操作");
        }
        $platform  = new \app\admin\model\Platform();
        $platformInfo =  $platform->where(['id' => $orderInfo['platform_id']])->find();
        $storeModel = new \app\admin\model\Store();
        $storeInfo = $storeModel->where(['id'=> $orderInfo['store_id']])->find();

        $userModel = new \app\admin\model\User();
        $userInfo  = $userModel->where(['id' => $storeInfo['uid']])->find();
        $templateModel   = new \app\admin\model\Template();
        $templateInfo = $templateModel->where(['name'=>'ticket_review',
            'platform_id'=> $orderInfo['platform_id']])->find();
        $withdraw = new \app\admin\model\template\TicketReview();
        if ($templateInfo){
            if ($userInfo['openid']){
                $sendData = [
                    'template_id' => $templateInfo['template_id'],
                    'appid' => $platformInfo['xcx_appid'],
                    'appsecret' => $platformInfo['xcx_appsecret'],
                    'openid' => $userInfo['openid'],
                    'desc' => $param['subsidy_status'] == 4 ? '审核通过':'驳回'  ,
                    'name' => '平台补贴',
                    // 'store_name' => $storeInfo['store_name'],
                ];
                $withdraw->send($sendData);
            }

        }
        return msg([

        ], 0, "审核成功");
    }
}